 package moa.sequencestreams.generator;

import org.apache.commons.math.random.RandomDataImpl;


public class Choose {
	RandomDataImpl rd ;
		private int[] num;	// list of the positions
		private float[] rval;	// random value (used to get random ordering of the items)

	public Choose()
	{
		  //delete [] num;
		  //delete [] rval;
	}
	public  int pos(int i) 
	{ 	
		return num[i]; 
	}	// returns the i-th position
	
	public Choose(int n, int k)
	{
		rd = new RandomDataImpl();
		  int i, j, ival;
		  float fval;

		  num = new int [n];
		  rval = new float[n];

		  // associate a random value with each item
		  // also copy item into num
		  for (i = 0; i < n; i++)
		    {
		      rval[i] = (float) rd.nextUniform(0, 1); //.
		      num[i] = i;
		    }
		  
		  // sort num according to the values in rval
		  for (i = 0; i < n; i++ )
		    {
		      ival = num[i]; 
		      fval = rval[i];
		      for ( j = i; j > 0 && rval[j-1] > fval; j-- ) {
			  num[j] = num[j-1];
			  rval[j] = rval[j-1];
			}
		      num[j] = ival;
		      rval[j] = fval;
		    }

		  // resort first k num according to position
		  for (i = 0; i < k; i++ )
		    {
		      ival = num[i]; 
		      for ( j = i; j > 0 && num[j-1] > ival; j-- )
		    	  num[j] = num[j-1];
		      num[j] = ival;
		    }
		}
}
